<template>
  <div class="dashboard-editor-container">

    <el-row :gutter="60" style="padding:16px 16px 0;margin-bottom:32px;">
      <el-col :span="6">
        <div style="display: flex;align-items: center; justify-content: space-between; background: #FFF;height: 100px;border-radius: 8px;padding: 10px 20px;">
          <div>
            <span class="iconfont icon-1zhanghu" style="font-size: 40px;margin: 10px;" />
          </div>
          <div style="font-size: 20px;text-align: center;">
            <div style="margin-bottom: 5px;">已绑定账户</div>
            <div style="margin-top: 5px;">
              {{ userNums[0] }}
            </div>
          </div>
        </div>
      </el-col>
      <el-col :span="6">
        <div style="display: flex;align-items: center; justify-content: space-between; background: #FFF;height: 100px;border-radius: 8px;padding: 10px 20px;">
          <div>
            <span class="iconfont icon-guanliyuan" style="font-size: 40px;margin: 10px;" />
          </div>
          <div style="font-size: 20px;text-align: center;">
            <div style="margin-bottom: 5px;">管理员</div>
            <div style="margin-top: 5px;">
              {{ userNums[1] }}
            </div>
          </div>
        </div>
      </el-col>
      <el-col :span="6">
        <div style="display: flex;align-items: center; justify-content: space-between; background: #FFF;height: 100px;border-radius: 8px;padding: 10px 20px;">
          <div>
            <span class="iconfont icon-xuexiao_jiaoshi" style="font-size: 40px;margin: 10px;" />
          </div>
          <div style="font-size: 20px;text-align: center;">
            <div style="margin-bottom: 5px;">教师</div>
            <div style="margin-top: 5px;">
              {{ userNums[2] }}
            </div>
          </div>
        </div>
      </el-col>
      <el-col :span="6">
        <div style="display: flex;align-items: center; justify-content: space-between; background: #FFF;height: 100px;border-radius: 8px;padding: 10px 20px;">
          <div>
            <span class="iconfont icon-xueshengguanli" style="font-size: 50px;margin: 10px;" />
          </div>
          <div style="font-size: 20px;text-align: center;">
            <div style="margin-bottom: 5px;">学员</div>
            <div style="margin-top: 5px;">
              {{ userNums[3] }}
            </div>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-row style="background:#fff;padding:16px 16px 0;margin-bottom:32px;">
      <div id="courseSales" />
    </el-row>

    <el-row style="background:#fff;padding:16px 16px 0;margin-bottom:32px;">
      <div id="courseQuality" />
    </el-row>

    <el-row style="background:#fff;padding:16px 16px 0;margin-bottom:32px;">
      <div id="dailyLife" />
    </el-row>

  </div>
</template>

<script>
import { getCourseQuality, getCourseSales, getDailyLife, getUserNum } from '@/api/echarts'

export default {
  name: 'DashboardAdmin',
  data() {
    return {
      userNums: [],
      dailyLife: {},
      courseSales: {},
      courseQuality: {}
    }
  },
  mounted() {
    this.getDailyLife()
    this.getCourseSales()
    this.getCourseQuality()
  },
  created() {
    this.getUserNum()
  },
  methods: {
    // 获取用户系统账户数据
    getUserNum() {
      getUserNum().then(res => {
        this.userNums = res.data
      })
    },
    // 获取七日内日活跃用户
    getDailyLife() {
      // 展示加载动画
      getDailyLife().then(res => {
        const xAxis = []
        const series = []
        res.data.forEach(item => {
          xAxis.push(item.name)
          series.push(item.counts)
        })
        this.dailyLife.xAxis = xAxis
        this.dailyLife.series = series
        this.initDailyLife()
      })
    },
    // 获取课程销量前五
    getCourseSales() {
      getCourseSales().then(res => {
        const xAxis = []
        const series = []
        res.data.forEach(item => {
          xAxis.push(item.name)
          series.push(item.counts)
        })
        this.courseSales.xAxis = xAxis
        this.courseSales.series = series
        this.initCourseSales()
      })
    },
    // 教学评价排行榜
    getCourseQuality() {
      getCourseQuality().then(res => {
        const xAxis = []
        const series = []
        res.data.forEach(item => {
          xAxis.push(item.name)
          series.push(item.counts)
        })
        this.courseQuality.xAxis = xAxis
        this.courseQuality.series = series
        this.initCourseQuality()
      })
    },
    initDailyLife() {
      const chartDom = document.getElementById('dailyLife')
      const myChart = this.$echarts.init(chartDom, null, {
        height: 350
      })
      window.onresize = function() {
        myChart.resize()
      }
      const option = {
        backgroundColor: '#FFF',
        title: {
          text: '近七日日活用户数',
          top: '15',
          left: 'center'
        },
        xAxis: {
          type: 'category',
          data: this.dailyLife.xAxis
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: this.dailyLife.series,
            type: 'line'
          }
        ],
        color: ['#ee6666']
      }
      myChart.setOption(option)
    },
    initCourseSales() {
      const chartDom = document.getElementById('courseSales')
      const myChart = this.$echarts.init(chartDom, null, {
        height: 350
      })
      window.onresize = function() {
        myChart.resize()
      }
      const option = {
        backgroundColor: '#FFF',
        title: {
          text: '课程销量排行榜',
          top: '15',
          left: 'center'
        },
        xAxis: {
          type: 'category',
          data: this.courseSales.xAxis
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: this.courseSales.series,
            type: 'line'
          }
        ],
        color: ['#91cc75']
      }
      myChart.setOption(option)
    },
    initCourseQuality() {
      const chartDom = document.getElementById('courseQuality')
      const myChart = this.$echarts.init(chartDom, null, {
        height: 350
      })
      window.onresize = function() {
        myChart.resize()
      }
      const option = {
        backgroundColor: '#FFF',
        title: {
          text: '课程好评排行榜',
          top: '15',
          left: 'center'
        },
        xAxis: {
          type: 'category',
          data: this.courseQuality.xAxis
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: this.courseQuality.series,
            type: 'line'
          }
        ],
        color: ['#fac858']
      }
      myChart.setOption(option)
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  position: relative;

  .github-corner {
    position: absolute;
    top: 0;
    border: 0;
    right: 0;
  }

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}

@media (max-width:1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>
